package com.ys.web.mobao.mapper;

import com.ys.base.BaseDao;
import com.ys.web.mobao.model.po.BindCard;
import com.ys.web.mobao.model.po.SystemBankPO;
import com.ys.web.pay.model.vo.UserBank;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface MobaoBindCardMapper extends BaseDao {
    @Select("select count(card_no) from user_bank where card_no=#{cardNo}")
    int getUserBankByCardNo(String cardNo);

    @Select("select a.*,b.bank_url_img,ifnull(b.color,'#DC143C') bankColor from user_bank a left join s_sys_bank b on a.bank_code = b.bank_code where user_id= #{uid}")
    List<UserBank> getUserBankByUserId(@Param("uid")long uid);

    @Select("select * from user_bank where card_token=#{token}")
    UserBank getUserBankByToken(String token);

//    @Insert("insert into user_bank(user_id,mobile,user_name,id_card_num,card_no,bank_name,card_token,bank_code)" +
//            "values(#{userId},#{mobile},#{userName},#{idCardNum},#{cardNo},#{bankName},#{cardToken},#{bankCode})")
    @Insert("INSERT INTO s_sys_bank(bank_name,bank_code,remark,supportCreditCard,supportDebitCard,protocolType) VALUES(#{bankName},#{code},#{remark},#{supportCreditCard},#{protocolType})")
    int saveSysBank(Map<String,Object> bank);

    @Insert("insert into user_bank(user_id,mobile,user_name,id_card_num,card_no,bank_name,card_token,out_trade_no,bank_type,linkId,cardType,bank_code)" +
            "values(#{userId},#{mobile},#{userName},#{idCardNum},#{cardNo},#{bankName},#{cardToken},#{requestId},#{bankType},#{linkId},#{cardType},#{bankCode})")
    void saveUserBank(BindCard bindCard);
    @Insert("insert into user_bank_mb(user_id,mobile,user_name,id_card_num,card_no,bank_name,card_token,out_trade_no,bank_type,linkId,cardType,bank_code)" +
            "values(#{userId},#{mobile},#{userName},#{idCardNum},#{cardNo},#{bankName},#{cardToken},#{requestId},#{bankType},#{linkId},#{cardType},#{bankCode})")
    void saveUserBankMb(BindCard bindCard);
    @Delete("delete from user_bank where user_id=#{uid} and out_trade_no=#{outTradNo}")
    int delUserBankByOutTradNo(@Param("uid")long uid, @Param("cardToken") String outTradNo);

    @Delete("delete from user_bank where user_id=#{uid} and card_token=#{cardToken}")
    int delUserBankByCardToken(@Param("uid")long uid, @Param("cardToken") String cardToken);

    @Select("select * from s_sys_bank where `status` = 1")
    List<SystemBankPO> getAll();

    @Update("update user_bank set linkId=#{linkId} where out_trade_no=#{requestNo}" )
    int updateLinkId(@Param("requestNo")String requestNo, @Param("linkId")String linkId);
    @Delete("delete from user_bank where requestNo=#{requestNo" )
    int delBindCard(@Param("requestNo")String requestNo);

    @Select("select * from user_bank where user_id=#{uid} and card_token = #{token}")
    UserBank getUserBank(@Param("uid")String uid,@Param("token")String token);

    @Update("update  user_bank set cardType = #{cardType},linkId=#{linkId},bank_name = #{bank_name} where out_trade_no=#{requestNo}" )
    int updateBindCard(@Param("requestNo")String requestNo,
                       @Param("cardType")String cardType,
                       @Param("linkId")String linkId,
                       @Param("bank_name")String bank_name);

    @Delete("delete from user_bank_mb where user_id=#{uid} and card_token=#{cardToken}")
    int delUserBankByCardTokenMb(@Param("uid")long uid, @Param("cardToken") String cardToken);
    @Select("select a.*,b.bank_url_img,ifnull(b.color,'#DC143C') bankColor from user_bank_mb a left join s_sys_bank_mb b on a.bank_code = b.bank_code where user_id= #{uid}")
    List<UserBank> getUserBankByUserIdMb(@Param("uid")long uid);
    @Select("select * from s_sys_bank_mb where `status` = 1")
    List<SystemBankPO> getAllMb();
    @Select("select count(card_no) from user_bank_mb where card_no=#{cardNo}")
    int getUserBankCheckCardNo(String cardNo);
}
